package com.zhai.netty.thread.chapter02;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class CurrentThread {
	
	/**
	 * 创建两个线程
	 */
	ScheduledExecutorService executor = Executors.newScheduledThreadPool(100);
	
	/**
	 *定义一个内存变量
	 */
	Map<String, Object> memoryMap = new ConcurrentHashMap<String, Object>();
	
	/**
	 * 定义一百个豆子
	 */
	protected volatile int bean = 100;
	
	
	public List<Thread> createThread(int num){
		List<Thread> threads = new LinkedList<Thread>();
		for(int i =0;i<num;i++){
			Thread thread = new Thread(new Runnable() {
				public void run() {
					/**
					 * 创建的线程都去抢豆子
					 */
					bean -=1;
					System.out.println("当前bena数为："+bean);
				}
			});
			thread.setName("我的第"+i+"个线程");
			memoryMap.put(thread.getName(),bean);
			threads.add(thread);
		}
		return threads;
	}
	
	public static void main(String[] args) {
		CurrentThread currentThread = new CurrentThread();
		List<Thread> threads = currentThread.createThread(100);
		for(int i=0;i<threads.size();i++){
			currentThread.executor.execute(threads.get(i));
			currentThread.memoryMap.get("我的第"+i+"个线程");
		}
	}
}
